let localContext = wx.getStorageSync('localContext');
const DEFAULT_PAGE = 0;

Component({
  startPageX: 0,
  currentView: DEFAULT_PAGE,
  behaviors: [],
  properties: {
    tabId: {
      type: String,
      value: ""
    }
  },
  data: {
    toView: `card+${DEFAULT_PAGE}`,
    imageList: []
  },
  methods: {
    getDiscoveryDetail(){
      let that = this;
      const db = wx.cloud.database();
      console.log("id:" + that.data.tabId);
      db.collection("images")
        .where({
          species: that.data.tabId
        })
        .get({
          success:function(res) {
            if(res&&res.data&&res.data.length){
              console.log(res.data);
              that.setData({
                imageList: res.data
              })
            }

          },
          fail: function(event) {
            console.error(event);
          }
        })
    }
  },
  ready(){
    if(!localContext || !localContext.dicoveryDetail){
      this.getDiscoveryDetail();
    }

  },
  touchStart(e){
      this.startPageX = e.changedTouches[0].pageX;
    },
  touchEnd(e){
      const moveX = e.changedTouches[0].pageX - this.startPageX;
      const maxPage = this.data.list.length - 1;
      if(Math.abs(moveX) >= 150) {
          if( moveX > 0) {
              this.currentView = this.currentView != 0 ? this.currentView - 1: 0;
          } else {
              this.currentView = this.currentView != maxPage ? this.currentView + 1 : maxPage;
          }
      }
      this.setData({
          toView: `card_${this.currentView}`
      });
  }
});